package com.jack.leetcode.eachProblem06;

import java.util.ArrayList;
import java.util.List;

public class 移除重复节点 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static ListNode removeDuplicateNodes(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode result = head;
        List<Integer> deduplicateList = new ArrayList<>();
        deduplicateList.add(head.val);
        ListNode next = head.next;
        while (head != null && next != null) {
            if (deduplicateList.contains(next.val)) {
                next = next.next;
                if (next == null) {
                    head.next = null;
                }
            } else {
                deduplicateList.add(next.val);
                head.next = next;
                head = head.next;
                next = next.next;
            }

        }
        return result;
    }
}
